/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf;

import com.controllers.CompanyController;
import com.controllers.CompanyDenominationController;
import com.controllers.CrudUtil;
import com.controllers.DenominationController;
import com.entities.Company;
import com.entities.CompanyDenomination;
import com.entities.Denomination;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

/**
 *
 * @author ben
 */
@Named(value = "companyDenominationAction")
@SessionScoped
public class CompanyDenominationAction implements Serializable {

    private static final Logger logger = Logger.getLogger(CompanyDenominationAction.class.getName());
    /**
     * Creates a new instance of CompanyDenominationAction
     */
    @EJB
    private CrudUtil crudUtil;
    @EJB
    private CompanyDenominationController companyDenominationController;
    @EJB
    private CompanyController companyController;
    @EJB
    private DenominationController denominationController;
    @ManagedProperty(value = "creditView")
    CreditView creditView;
    private Company company;
    private Company comp = new Company();
    private Denomination denomination;

    public void deleteCompanyValue(Long id) {
        try {
            creditView.companyTab(0);
            comp = companyController.findCompany(id);
            if (comp != null) {
                if (crudUtil.delete(comp)) {
                    logger.info("Successfully Deleted");
                } else {
                    logger.warning("Unable to delete Object");
                }
            }
        } catch (Exception e) {
            logger.log(Level.WARNING, "Error Occured at: {0}", e.getMessage());
        }
    }

    public String updateCompanyDetails(Long id) {
        comp = new Company();
        try {
            comp = companyController.findCompany(id);
            if (comp != null) {
                comp.getId();
                logger.log(Level.INFO, "Found and is: {0}", id);
                return "edit_company.xhtml?faces-redirect=true";
            } else {
                return "index.xhtml?faces-redirect=true";
            }
        } catch (Exception e) {
            logger.log(Level.WARNING, "Error Occured at: {0}", e.getMessage());
            return null;
        }
    }

    public String actualUpdateCompanyDetails() {
        try {
            creditView.companyTab(0);
            comp.getId();
            if (crudUtil.update(comp)) {
                logger.info("Successfully Updated");
                return "index.xhtml?faces-redirect=true";
            } else {
                return "edit_company.xhtml?faces-redirect=true";
            }
        } catch (Exception e) {
            logger.log(Level.WARNING, "Error Occured at: {0}", e.getMessage());
            return "edit_company.xhtml?faces-redirect=true";
        }
    }

    public String saveCompanyDenomination() {
        //creditView.vouchertab(2);
        Company company1 = companyController.findCompany(selectedCompany);
        Denomination denomination1 = denominationController.findDenomination(selectedDenomination);

        if (company1 != null && denomination1 != null) {
            CompanyDenomination companyDenomination = new CompanyDenomination();
            companyDenomination.setCompany(company1);
            companyDenomination.setDenomination(denomination1);

            Long result = companyDenominationController.save(companyDenomination);
            //creditView.setActiveIndex(2);

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saving Voucher Successful", "Saving Voucher Successful"));
            companyDenomination = new CompanyDenomination();
            return "index";
        } else {

            return "error";

        }
    }

    public CompanyDenominationAction() {
        this.company = new Company();
        this.denomination = new Denomination();
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public Denomination getDenomination() {
        return denomination;
    }

    public void setDenomination(Denomination denomination) {
        this.denomination = denomination;
    }
    private Long selectedCompany;

    public Long getSelectedCompany() {
        return selectedCompany;
    }

    public void setSelectedCompany(Long selectedCompany) {
        this.selectedCompany = selectedCompany;
    }
    private Long selectedDenomination;

    public Long getSelectedDenomination() {
        return selectedDenomination;
    }

    public void setSelectedDenomination(Long selectedDenomination) {
        this.selectedDenomination = selectedDenomination;
    }

    public CrudUtil getCrudUtil() {
        return crudUtil;
    }

    public void setCrudUtil(CrudUtil crudUtil) {
        this.crudUtil = crudUtil;
    }

    public CompanyDenominationController getCompanyDenominationController() {
        return companyDenominationController;
    }

    public void setCompanyDenominationController(CompanyDenominationController companyDenominationController) {
        this.companyDenominationController = companyDenominationController;
    }

    public CompanyController getCompanyController() {
        return companyController;
    }

    public void setCompanyController(CompanyController companyController) {
        this.companyController = companyController;
    }

    public DenominationController getDenominationController() {
        return denominationController;
    }

    public void setDenominationController(DenominationController denominationController) {
        this.denominationController = denominationController;
    }

    public Company getComp() {
        return comp;
    }

    public void setComp(Company comp) {
        this.comp = comp;
    }

    public CreditView getCreditView() {
        return creditView;
    }

    public void setCreditView(CreditView creditView) {
        this.creditView = creditView;
    }
}
